{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.datasets import make_blobs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X,y = make_blobs(n_samples=70,centers=4)\n",
    "data = np.array(X,dtype=np.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6.14542897,  0.33926704],\n",
       "       [ 7.4388761 ,  3.03455035],\n",
       "       [ 4.03304582, -2.13543467],\n",
       "       [-8.90049761, -3.83146094],\n",
       "       [ 5.19515946,  2.67621747],\n",
       "       [ 9.05788432,  1.44449999],\n",
       "       [ 5.76045682,  0.2829069 ],\n",
       "       [-8.97223827, -3.19091506],\n",
       "       [11.41595866,  2.89756029],\n",
       "       [-9.61330794, -6.86135099]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0:10][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(70, 2)\n",
      "8\n",
      "140\n",
      "float64\n"
     ]
    }
   ],
   "source": [
    "print(data.ndim)\n",
    "print(data.shape)\n",
    "print(data.itemsize)\n",
    "print(data.size)\n",
    "print(data.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  0],\n",
       "       [ 7,  3],\n",
       "       [ 4, -2],\n",
       "       [-8, -3],\n",
       "       [ 5,  2],\n",
       "       [ 9,  1],\n",
       "       [ 5,  0],\n",
       "       [-8, -3],\n",
       "       [11,  2],\n",
       "       [-9, -6]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int_data = data.astype(np.int32) # change the dtype and return a new one\n",
    "int_data[:10][:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 3],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.full((2,3),3)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  9, 11, 13])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy.arange(start, stop, step, dtype)\n",
    "data2 = np.arange(1,15,2)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0., 1., 2., 3., 4., 5., 6., 7., 8.])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.linspace(0,8,9)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.463393  , 0.57628205, 0.65361153, 0.28803778, 0.89771492],\n",
       "       [0.69781381, 0.34417304, 0.84372902, 0.65037703, 0.32916258],\n",
       "       [0.57844626, 0.97158874, 0.98426718, 0.96902471, 0.53390048],\n",
       "       [0.50959498, 0.07537521, 0.25386625, 0.58522587, 0.40715816],\n",
       "       [0.29157655, 0.05098765, 0.80744421, 0.72667282, 0.11691093]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.random.rand(5,5)\n",
    "data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 6, 2, 8],\n",
       "       [6, 3, 8, 6, 3],\n",
       "       [5, 9, 9, 9, 5],\n",
       "       [5, 0, 2, 5, 4],\n",
       "       [2, 0, 8, 7, 1]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 *= 10\n",
    "data2.astype(np.int32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[25, 33, 35, 26, 11],\n",
       "       [21, 14, 28, 16, 29],\n",
       "       [30, 27, 21, 17, 27],\n",
       "       [24, 24, 34, 22, 21]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 = np.random.randint(low=10,high=40,size=(4,5))\n",
    "data2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Operate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 10,  26,  30,  12, -18],\n",
       "       [  2, -12,  16,  -8,  18],\n",
       "       [ 20,  14,   2,  -6,  14],\n",
       "       [  8,   8,  28,   4,   2]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2 * 2 - 40"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 0 8]\n",
      " [3 5 3]\n",
      " [6 0 2]]\n",
      "\n",
      "[[5 4]\n",
      " [4 8]\n",
      " [1 5]]\n",
      "\n",
      "[[33 60]\n",
      " [38 67]\n",
      " [32 34]]\n"
     ]
    }
   ],
   "source": [
    "d_1 = np.random.randint(0,9,(3,3))\n",
    "d_2 = np.random.randint(0,9,(3,2))\n",
    "print(d_1)\n",
    "print()\n",
    "print(d_2)\n",
    "print()\n",
    "print(np.dot(d_1,d_2)) # 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "485"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一元操作\n",
    "data2.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[25 33 35 26 11]\n",
      " [21 14 28 16 29]\n",
      " [30 27 21 17 27]\n",
      " [24 24 34 22 21]]\n",
      "[21 14 21 16 11]\n",
      "[11 14 17 21]\n"
     ]
    }
   ],
   "source": [
    "print(data2)\n",
    "print(data2.min(axis=0))\n",
    "print(data2.min(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[11, 25, 26, 33, 35],\n",
       "       [14, 16, 21, 28, 29],\n",
       "       [17, 21, 27, 27, 30],\n",
       "       [21, 22, 24, 24, 34]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data2_cy = data2.copy()\n",
    "print(id(data2)==id(data2_cy))\n",
    "data2_cy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 7]\n",
      " [3 7 3 6]\n",
      " [5 1 3 7]]\n",
      "[[5 7 5 3]\n",
      " [2 6 1 6]\n",
      " [3 7 5 6]]\n"
     ]
    }
   ],
   "source": [
    "d = np.random.randint(0,9,size=(3,4))\n",
    "d2 = np.random.randint(0,9,size=(3,4))\n",
    "print(d)\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3 7]\n",
      " [3 3 6 7]\n",
      " [1 3 5 7]]\n",
      "[[3 5 5 7]\n",
      " [1 2 6 6]\n",
      " [3 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "d.sort\n",
    "d2.sort(axis=1)\n",
    "print(d)\n",
    "print(d2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 通用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1, -7,  0, -8],\n",
       "       [ 1,  5, -3,  3],\n",
       "       [-6,  6,  4,  7],\n",
       "       [ 0,  6,  3, -7]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.random.randint(-9,9,size=(4,4))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  2, -14,   0, -16],\n",
       "       [  2,  10,  -6,   6],\n",
       "       [-12,  12,   8,  14],\n",
       "       [  0,  12,   6, -14]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(d,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1, -1,  0, -1],\n",
       "       [ 1,  1, -1,  1],\n",
       "       [-1,  1,  1,  1],\n",
       "       [ 0,  1,  1, -1]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sign(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.0625, -0.4375,  0.    , -0.5   ],\n",
       "       [ 0.0625,  0.3125, -0.1875,  0.1875],\n",
       "       [-0.375 ,  0.375 ,  0.25  ,  0.4375],\n",
       "       [ 0.    ,  0.375 ,  0.1875, -0.4375]])"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.divide(d,4)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.06, -0.44,  0.  , -0.5 ],\n",
       "       [ 0.06,  0.31, -0.19,  0.19],\n",
       "       [-0.38,  0.38,  0.25,  0.44],\n",
       "       [ 0.  ,  0.38,  0.19, -0.44]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = np.around(d,2)\n",
    "d"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Index and Slice"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[43, 59, 86, 84, 84],\n",
       "       [41, 59, 45, 41, 75],\n",
       "       [49, 33, 55, 71, 44],\n",
       "       [20, 88, 72, 17, 56],\n",
       "       [19, 44, 64, 18, 11]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(10,90,size=(5,5))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 59,  0, 84, 84],\n",
       "       [ 0, 59,  0, 41, 75],\n",
       "       [ 0, 33,  0, 71, 44],\n",
       "       [ 0, 88,  0, 17, 56],\n",
       "       [ 0, 44,  0, 18, 11]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:,:4:2] = 0 # 在原生数组上做修改\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Delete、Add、Remove Repetition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 59,  0, 84, 84,  0, 59,  0, 41, 75,  0, 33,  0, 71, 44,  0, 88,\n",
       "        0, 17, 56,  0, 44,  0, 18, 11, 15, 22, 14, 76, 44])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append(data,[15,22,14,76,44]) # one-dimmension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 59,  0, 84, 84],\n",
       "       [ 0, 59,  0, 41, 75],\n",
       "       [ 0, 33,  0, 71, 44],\n",
       "       [ 0, 88,  0, 17, 56],\n",
       "       [ 0, 44,  0, 18, 11]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data # no change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 59,  0, 84, 84],\n",
       "       [ 0, 59,  0, 41, 75],\n",
       "       [ 0, 33,  0, 71, 44],\n",
       "       [ 0, 88,  0, 17, 56],\n",
       "       [ 0, 44,  0, 18, 11],\n",
       "       [15, 22, 14, 76, 44]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.append(data,[[15,22,14,76,44]],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 59,  0, 84, 84],\n",
       "       [ 0, 59,  0, 41, 75],\n",
       "       [ 0, 33,  0, 71, 44],\n",
       "       [-1, -1, -1, -1, -1],\n",
       "       [ 0, 88,  0, 17, 56],\n",
       "       [ 0, 44,  0, 18, 11]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(data,3,[-1,-1,-1,-1,-1],axis=0) # along row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, 59,  0, -1, 84, 84],\n",
       "       [ 0, 59,  0, -1, 41, 75],\n",
       "       [ 0, 33,  0, -1, 71, 44],\n",
       "       [ 0, 88,  0, -1, 17, 56],\n",
       "       [ 0, 44,  0, -1, 18, 11]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.insert(data,3,[-1,-1,-1,-1,-1],axis=1) # along colunm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [ 5,  6,  7,  8,  9],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.arange(15).reshape(3,5)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [10, 11, 12, 13, 14]])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(data,1,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  3,  4],\n",
       "       [ 5,  6,  8,  9],\n",
       "       [10, 11, 13, 14]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.delete(data,2,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[14, 15, 13, 16],\n",
       "       [12, 18, 19, 14],\n",
       "       [14, 14, 14, 13],\n",
       "       [12, 17, 12, 10]])"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(10,20,size=(4,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 12, 13, 14, 15, 16, 17, 18, 19])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 6, 6, 6],\n",
       "       [8, 5, 6, 5],\n",
       "       [7, 7, 8, 7],\n",
       "       [5, 8, 5, 5]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(5,9,size=(4,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 8, 5, 5],\n",
       "       [7, 7, 8, 7],\n",
       "       [8, 5, 6, 5],\n",
       "       [8, 6, 6, 6]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 6, 6, 8],\n",
       "       [5, 5, 6, 8],\n",
       "       [7, 7, 8, 7],\n",
       "       [5, 8, 5, 5]])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 2, 1],\n",
       "       [1, 1, 1]])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.array([[1,1,1],[1,2,1],[1,1,1]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [1, 2, 1]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [1, 2],\n",
       "       [1, 1]])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(data,axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Shape Changing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 5)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[4.77, 5.12, 7.26, 4.07, 8.58],\n",
       "       [5.61, 8.41, 5.34, 7.39, 9.15],\n",
       "       [3.89, 1.34, 8.73, 4.16, 5.98],\n",
       "       [3.41, 5.85, 3.94, 6.94, 2.52],\n",
       "       [7.56, 9.81, 0.82, 5.49, 5.07]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.around(10*np.random.random((5,5)),2)\n",
    "print(data.shape)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = data.astype(np.int32)\n",
    "data = np.delete(data,[3,4],axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 7, 5, 8, 5, 3, 1, 8, 3, 5, 3, 7, 9, 0])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.ravel() # level off"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 7, 5, 8],\n",
       "       [5, 3, 1, 8, 3],\n",
       "       [5, 3, 7, 9, 0]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.reshape(3,5) # old array does not encounter change"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 5, 3, 3, 7],\n",
       "       [5, 8, 1, 5, 9],\n",
       "       [7, 5, 8, 3, 0]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T # 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 3, 8, 9, 8],\n",
       "       [5, 7, 1, 7, 3],\n",
       "       [3, 5, 5, 5, 0]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.resize(3,5) # old array encounters change\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## stack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 5]\n",
      " [4 2]]\n",
      "[[-4 -6]\n",
      " [-5 -1]]\n"
     ]
    }
   ],
   "source": [
    "d_1 = np.array([[1,5],[4,2]])\n",
    "d_2 = np.array([[-4,-6],[-5,-1]])\n",
    "print(d_1)\n",
    "print(d_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  5, -4, -6],\n",
       "       [ 4,  2, -5, -1]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((d_1,d_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  5],\n",
       "       [ 4,  2],\n",
       "       [-4, -6],\n",
       "       [-5, -1]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((d_1,d_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 2, 1)\n",
      "[[[1]\n",
      "  [5]]\n",
      "\n",
      " [[4]\n",
      "  [2]]]\n"
     ]
    }
   ],
   "source": [
    "d_3 = d_1[:,:,np.newaxis]\n",
    "print(d_3.shape)\n",
    "print(d_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 分割数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  2,  7,  6],\n",
       "       [ 3, -4, -6,  8],\n",
       "       [-6, -1,  2, -7],\n",
       "       [ 1, -4, -1,  7]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target = np.random.randint(-9,9,size=(4,4))\n",
    "target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6  2]\n",
      " [ 3 -4]\n",
      " [-6 -1]\n",
      " [ 1 -4]]\n",
      "[[ 7  6]\n",
      " [-6  8]\n",
      " [ 2 -7]\n",
      " [-1  7]]\n"
     ]
    }
   ],
   "source": [
    "t = np.hsplit(target,2)\n",
    "t1 = t[0]\n",
    "t2 = t[1]\n",
    "print(t1)\n",
    "print(t2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 6  2]\n",
      " [ 3 -4]] \n",
      " [[ 7  6]\n",
      " [-6  8]]\n",
      "[[-6 -1]\n",
      " [ 1 -4]] \n",
      " [[ 2 -7]\n",
      " [-1  7]]\n"
     ]
    }
   ],
   "source": [
    "# split the arrary into four square-equal arrays\n",
    "t3 = np.vsplit(t1,2)[1]\n",
    "t1 = np.vsplit(t1,2)[0]\n",
    "t4 = np.vsplit(t2,2)[1]\n",
    "t2 = np.vsplit(t2,2)[0]\n",
    "print(t1,\"\\n\",t2)\n",
    "print(t3,\"\\n\",t4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 复制和视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 7],\n",
       "       [5, 4, 8, 7],\n",
       "       [6, 6, 8, 6],\n",
       "       [7, 8, 7, 8]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(4,9,size=(4,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 7],\n",
       "       [5, 4, 8, 7],\n",
       "       [6, 6, 8, 6],\n",
       "       [7, 8, 7, 8]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_view = data.view()\n",
    "data_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 7, 1],\n",
       "       [5, 4, 8, 7, 1],\n",
       "       [6, 6, 8, 6, 1],\n",
       "       [7, 8, 7, 8, 1]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = [[1],[1],[1],[1]]\n",
    "data_view = np.append(data_view,t,axis=1)\n",
    "data_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 4, 5, 7],\n",
       "       [5, 4, 8, 7],\n",
       "       [6, 6, 8, 6],\n",
       "       [7, 8, 7, 8]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data is data_view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# due to insertion,data_view is not the view of data\n",
    "# 理解view，只需要注意两者共享数据，此外其它所有都是独立的。\n",
    "data_view.base is data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# deeply deplicate\n",
    "data = np.arange(10)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, -1, -1, -1,  4,  5,  6,  7,  8,  9])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_cp = data.copy()\n",
    "data_cp[1:4] = -1\n",
    "data_cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 2, 2],\n",
       "       [2, 2, 2, 2],\n",
       "       [2, 2, 2, 2],\n",
       "       [2, 2, 2, 2]])"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.full((4,4),2)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 4, 5, 6],\n",
       "       [3, 4, 5, 6],\n",
       "       [3, 4, 5, 6],\n",
       "       [3, 4, 5, 6]])"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = np.arange(1,5)\n",
    "data + test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3, 3, 3, 3],\n",
       "       [4, 4, 4, 4],\n",
       "       [5, 5, 5, 5],\n",
       "       [6, 6, 6, 6]])"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = np.arange(1,5).reshape(4,1)\n",
    "# print(test)\n",
    "data + test"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-8,  5, -1,  0],\n",
       "       [-1,  8, -8,  0],\n",
       "       [ 8,  7, -7, -7],\n",
       "       [-9, -6, -9,  8]])"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# array\n",
    "data = np.random.randint(-9,9,size=(4,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1,  8, -8,  0],\n",
       "       [-9, -6, -9,  8]])"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[[1,3]] # 一维索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1, -9])"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[[1,3],[0,2]] # 二维索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[-1,  8, -8,  0],\n",
       "        [-9, -6, -9,  8]],\n",
       "\n",
       "       [[-8,  5, -1,  0],\n",
       "        [ 8,  7, -7, -7]]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1,3],[0,2]]) # 二维索引\n",
    "data[a]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-8,  5, -1,  0],\n",
       "       [-1,  8, -8,  0],\n",
       "       [ 8,  7, -7, -7],\n",
       "       [-9, -6, -9,  8]])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False,  True, False,  True],\n",
       "       [False,  True, False,  True],\n",
       "       [ True,  True, False, False],\n",
       "       [False, False, False,  True]])"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = data >= 0\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 0, 8, 0, 8, 7, 8])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[b]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-8,  7, -1,  2],\n",
       "       [-1, 10, -8,  2],\n",
       "       [10,  9, -7, -7],\n",
       "       [-9, -6, -9, 10]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[b] += 1\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1,  2, -1,  2])"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[(data>=-2) & (data<=2)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  0, -2, -2],\n",
       "       [ 0, -2,  1, -1],\n",
       "       [-1,  0, -1,  1],\n",
       "       [ 1, -2,  0, -2]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(-2,2,size=(4,4))\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3], dtype=int64),\n",
       " array([2, 3, 1, 2, 3, 0, 2, 3, 0, 1, 3], dtype=int64))"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.nonzero() # save the index of non-zore values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-2, -2, -2,  1, -1, -1, -1,  1,  1, -2, -2])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[data.nonzero()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[74 71 90 85 85 78]\n",
      " [77 97 91 95 72 83]\n",
      " [84 89 99 84 86 89]\n",
      " [83 75 80 99 96 81]\n",
      " [84 79 98 96 73 92]]\n",
      "[[54 65 55 60 63 52]\n",
      " [56 51 53 64 60 54]\n",
      " [68 62 55 64 67 60]\n",
      " [67 67 64 52 61 68]\n",
      " [51 58 65 67 68 54]\n",
      " [61 64 52 53 56 57]\n",
      " [68 66 61 52 53 60]]\n"
     ]
    }
   ],
   "source": [
    "# 分析学生的成绩\n",
    "excellent = np.random.randint(70,100,size=(5,6))\n",
    "poor = np.random.randint(50,69,size=(7,6))\n",
    "print(excellent)\n",
    "print(poor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[74, 71, 90, 85, 85, 78],\n",
       "       [77, 97, 91, 95, 72, 83],\n",
       "       [84, 89, 99, 84, 86, 89],\n",
       "       [83, 75, 80, 99, 96, 81],\n",
       "       [84, 79, 98, 96, 73, 92],\n",
       "       [54, 65, 55, 60, 63, 52],\n",
       "       [56, 51, 53, 64, 60, 54],\n",
       "       [68, 62, 55, 64, 67, 60],\n",
       "       [67, 67, 64, 52, 61, 68],\n",
       "       [51, 58, 65, 67, 68, 54],\n",
       "       [61, 64, 52, 53, 56, 57],\n",
       "       [68, 66, 61, 52, 53, 60]])"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu = np.vstack((excellent,poor))\n",
    "stu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([483, 515, 531, 514, 522, 349, 338, 376, 379, 363, 343, 360])"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "total = stu.sum(axis=1)\n",
    "total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(338, 531)"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lowest = total.min()\n",
    "highest = total.max()\n",
    "lowest,highest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[84, 89, 99, 84, 86, 89],\n",
       "       [56, 51, 53, 64, 60, 54]])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu[(total == highest) | (total == lowest)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 3, 1, 4, 2, 10, 12, 7, 6, 8, 11, 9]"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 排名\n",
    "n = stu.shape[0] # the number of students\n",
    "total_cp = total.copy()\n",
    "rank = [0 for i in range(n)]\n",
    "\n",
    "def get_rank(n):\n",
    "    i = 0\n",
    "    for k in range(1,n):\n",
    "        if total_cp[i] < total_cp[k]:\n",
    "            i = k\n",
    "    total_cp[i] = 0\n",
    "    return i\n",
    "\n",
    "for i in range(n):\n",
    "    rank[get_rank(n)] = i+1\n",
    "rank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  5, 483,  74,  71,  90,  85,  85,  78],\n",
       "       [  3, 515,  77,  97,  91,  95,  72,  83],\n",
       "       [  1, 531,  84,  89,  99,  84,  86,  89],\n",
       "       [  4, 514,  83,  75,  80,  99,  96,  81],\n",
       "       [  2, 522,  84,  79,  98,  96,  73,  92],\n",
       "       [ 10, 349,  54,  65,  55,  60,  63,  52],\n",
       "       [ 12, 338,  56,  51,  53,  64,  60,  54],\n",
       "       [  7, 376,  68,  62,  55,  64,  67,  60],\n",
       "       [  6, 379,  67,  67,  64,  52,  61,  68],\n",
       "       [  8, 363,  51,  58,  65,  67,  68,  54],\n",
       "       [ 11, 343,  61,  64,  52,  53,  56,  57],\n",
       "       [  9, 360,  68,  66,  61,  52,  53,  60]])"
      ]
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_rank = np.insert(stu,0,rank,axis=1)\n",
    "score_rank = np.insert(score_rank,1,total,axis=1)\n",
    "score_rank"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1, 531,  84,  89,  99,  84,  86,  89],\n",
       "       [  2, 522,  84,  79,  98,  96,  73,  92]])"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score_rank[[2,4]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 统计方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([80.5, 85.8, 88.5, 85.7, 87. , 58.2, 56.3, 62.7, 63.2, 60.5, 57.2,\n",
       "       60. ])"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean = stu.mean(axis=1)\n",
    "mean = np.around(mean,1)\n",
    "mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 189,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([44.9, 85.5, 26.2, 76.6, 82.7, 23.1, 19.6, 19.2, 30.5, 42.9, 17.8,\n",
       "       35.7])"
      ]
     },
     "execution_count": 189,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var = np.around(stu.var(axis=1),1)\n",
    "var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 191,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([90, 97, 99, 99, 98, 65, 64, 68, 68, 68, 64, 68])"
      ]
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max_ = stu.max(axis=1)\n",
    "max_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([71, 72, 84, 75, 73, 52, 51, 55, 52, 51, 52, 52])"
      ]
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min_ = stu.min(axis=1)\n",
    "min_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.35772752, 0.84008344, 0.45145357, 0.3834301 , 0.29613925])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rd = np.random.random(5) # generate five random numbers\n",
    "rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.59206985, 0.05790531, 0.21418178, 0.74024409, 0.11551207],\n",
       "       [0.84420211, 0.05335527, 0.2732089 , 0.2630698 , 0.36540618],\n",
       "       [0.42188679, 0.78249775, 0.1707733 , 0.75951065, 0.5644256 ]])"
      ]
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rd = np.random.random((3,5))\n",
    "rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.56035211, -0.0309776 , -0.62092842],\n",
       "       [-1.46458049,  1.41194612, -0.47673214]])"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(50) # set seed\n",
    "# 从均值0，方差1的正态分布中抽取样本\n",
    "rd = np.random.randn(2,3) # generate random numbers\n",
    "rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 197,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.76053669, 0.31000935, 0.3465412 ],\n",
       "       [0.35176482, 0.14546686, 0.97266468]])"
      ]
     },
     "execution_count": 197,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从均匀分布中抽取样本\n",
    "rd = np.random.rand(2,3) # generate random numbers\n",
    "rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.6316882 ,  5.31096858,  4.48368482],\n",
       "       [-0.67282592, -3.98763032,  8.99454147]])"
      ]
     },
     "execution_count": 198,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从期望为1，方差为5的正态分布中抽取随机样本\n",
    "rd = np.random.normal(1,5,(2,3))\n",
    "rd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0, 2, 6, 9, 4, 7, 8, 5, 1, 3]\n"
     ]
    }
   ],
   "source": [
    "arr = [i for i in range(10)]\n",
    "print(arr)\n",
    "np.random.shuffle(arr)\n",
    "print(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
